
#include "NNetwork.h"
#include <SFML\Graphics.hpp>
#include <process.h>
#include <stdio.h>
#include <time.h>

NNetwork * n;
void runNetwork()
{
	n->run();
}

int main()
{
	sf::RenderWindow * renderWindow =  new sf::RenderWindow(sf::VideoMode(512,512, 32),"Tripping balls");

	sf::Texture * texture = new sf::Texture();
	texture->Create(256,256);
	sf::Sprite * sprite = new sf::Sprite();

	sprite->SetTexture(*texture);
	sprite->SetScale(2,2);
	sprite->SetPosition(0,0);
	sf::Image img;
	img.Create(256,256);

	srand(time(0));
	n = new NNetwork(256);

	// Create a thread with our function
	sf::Thread Thread(&runNetwork);

	// Start it !
	Thread.Launch();

	
	while(renderWindow->IsOpen())
	{
		sf::Event e;
		while (renderWindow->PollEvent(e))
		{
			if ((e.Type == sf::Event::KeyPressed))
			{
				switch(e.Key.Code)
				{
				case(sf::Keyboard::Escape):
					renderWindow->Close();
					break;
				case(sf::Keyboard::Space):
					n->pushToStack(0,10);
				}
			}
		}

		for(int x = 0;x < 256;x++)
		{
			for(int y = 0;y < 256;y++)
			{
				int i = n->data[x+(y*256)];
				//std::cout << x << " " << y << " " << i << "\n";
				img.SetPixel(x,y,sf::Color(i,i,i,255));
				if(x+(y*256) == n->pos)
				{
					img.SetPixel(x,y,sf::Color::Red);
					if(n->iffy > 0)
						img.SetPixel(x,y,sf::Color::Green);

				}
			}
		}
		texture->LoadFromImage(img);

		renderWindow->Clear();
		renderWindow->Draw(*sprite);
		renderWindow->Display();

	}
}